package com.example.springboot159.springboot.chapter03;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SimpleInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiation(BeanDefinition definition, String beanName, Constructor ctor, Object[] args) throws Exception {
        try {
            Class clazz = definition.getBean();
            if (ctor != null) {
                return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
            }
            return clazz.getDeclaredConstructor().newInstance();
        } catch (InstantiationException |IllegalAccessException|IllegalArgumentException|NoSuchMethodException|InvocationTargetException|SecurityException e) {
           throw  new Exception("instantiation error");
        }
    }
}
